﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenChart.Entities;

namespace OpenChart.Check
{
    /// <summary>
    /// ChartArea的合法性检查类
    /// </summary>
    abstract class AbsChecking
    {
        /// <summary>
        /// 要对其检查的CartArea
        /// </summary>
        public ChartArea ChartArea { get; set; }

        /// <summary>
        /// 检查错误并抛出异常
        /// </summary>
        public void CheckAndThrowException()
        {
            this.CheckCommonException();//检查共性异常
            this.CheckChartTypeSpecificException();//检查特性异常
        }

        //检查所有类型Chart所可能产生的共同异常
        private void CheckCommonException()
        {
            if (this.ChartArea.Series.Count == 0)
            {
                throw new ChartException("ChartArea.Series.Count不能小于1");
            }
            if (this.ChartArea.AxisX.Interval == null
                && this.ChartArea.ChartType != Enums.ChartType.Pie)
            {
                throw new ChartException("X轴Interval属性必须设置");
            }
            if (this.ChartArea.AxisY1.Interval == null
                && this.ChartArea.ChartType != Enums.ChartType.Pie
                && this.ChartArea.ChartType != Enums.ChartType.LogarithmArea
                && this.ChartArea.ChartType != Enums.ChartType.LogarithmDot
                && this.ChartArea.ChartType != Enums.ChartType.LogarithmLine)
            {
                throw new ChartException("Y轴Interval属性必须设置");
            }
            if (this.ChartArea.AxisX.MinVAlue != null && this.ChartArea.AxisX.MaxValue != null)
            {
                if (this.ChartArea.AxisX.MinVAlue.Value > this.ChartArea.AxisX.MaxValue.Value)
                {
                    throw new ChartException("X轴的最大值不能小于最小值");
                }
            }
            if (this.ChartArea.AxisY1.MinVAlue != null && this.ChartArea.AxisY1.MaxValue != null)
            {
                if (this.ChartArea.AxisY1.MinVAlue.Value > this.ChartArea.AxisY1.MaxValue.Value)
                {
                    throw new ChartException("Y轴的最大值不能小于最小值");
                }
            }
            if (this.ChartArea.Data == null || this.ChartArea.Data.Rows.Count == 0)
            {
                throw new ChartException("Data属性不能为空且行数必须大于0");
            }
        }

        /// <summary>
        /// 检查特定类型的Chart所可能产生的异常
        /// </summary>
        protected abstract void CheckChartTypeSpecificException();
    }
}
